//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Media;
using OpenLS.UI.Interop;

namespace OpenLS.UI
{
    /// <summary>
    /// Interaction logic for SplashWindow.xaml
    /// </summary>

    public class SplashWindow : Window
    {

        public SplashWindow()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            ShowInTaskbar = false;
            WindowStyle = WindowStyle.None;
            ResizeMode = ResizeMode.NoResize;
            Topmost = true;
            Background = Brushes.White;
            var template = new ControlTemplate(typeof(SplashWindow))
                               {
                                   VisualTree = new FrameworkElementFactory(typeof (Grid))
                               };
            var b = new Binding
                        {
                            RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
                            Path = new PropertyPath("Background")
                        };
            template.VisualTree.SetBinding(Panel.BackgroundProperty, b);
            template.VisualTree.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter)));
            Template = template;
            BorderThickness = new Thickness();
            Loaded += OnLoaded;
            MouseDown += delegate { Close(); };

        }
        void OnLoaded(object sender, object args)
        {
            if (NativeMethods.IsCompositionEnabled())
            {
                System.IntPtr hwnd = (new WindowInteropHelper(this)).Handle;
                int style = NativeMethods.GetWindowLong(hwnd, WindowLong.GWL_STYLE);
                int style2 = style & ~(WS_CAPTION | WS_THICKFRAME);
                NativeMethods.SetWindowLong(hwnd, -16, style2   );
                HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
                NativeMethods.Margins pMargins = new NativeMethods.Margins();
                pMargins.Top = 0;
                NativeMethods.DwmExtendFrameIntoClientArea(hwnd, ref pMargins);
            }
        }

        const int WS_CAPTION          = 0x00C00000;
        const int WS_THICKFRAME = 0x00040000;


    }
}